﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Microsoft.Office;
using Application = System.Windows.Forms.Application;
using Microsoft.Office.Interop.Excel;
using System.Data;
namespace MINERP.COM
{
    public class DevGridViewExport
    {
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="inFileName">导出的文件名字 </param>
        /// <param name="inGridView">导出哪儿个grid</param>
        public static void ExportToExcel(string inFileName, DevExpress.XtraGrid.Views.Grid.GridView inGridView)
        {
            ExcelEdit excelEdit=null ;
            try
            {
                 excelEdit = new ExcelEdit();
                 try
                 {
                     excelEdit.Create();
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("未检测到系统安装office，请正确安装完整版office。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     return;
                 }
                 
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "电子表格(*.xls)|*.xls";
                //saveFileDialog.AddExtension = true;
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.Title = "导出Excel文件到";
                saveFileDialog.FileName = inFileName;
                if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string selectName = saveFileDialog.FileName;
                    Worksheet currSheet = excelEdit.AddSheet(inFileName);
                    excelEdit.SetCellTatolProperty(currSheet, 1, 1, 1, 1);//设置导出的文件的名字
                    excelEdit.UniteCells(currSheet, 1, 1,1 , inGridView.Columns.Count);
                    excelEdit.SetCellValue(currSheet, 1, 1, inFileName);
                    //写入列标题
                    for (int i = 0; i < inGridView.Columns.Count; i++)
                    {
                        excelEdit.SetCellValue(currSheet, 2, i + 1, inGridView.Columns[i].Caption);//设置标题

                    }
                    //写入值
                    for (int i = 0; i < inGridView.Columns.Count; i++)
                    {
                        for (int j = 0; j < inGridView.RowCount; j++)
                        {
                            var cellValue = inGridView.GetRowCellValue(j, inGridView.Columns[i]);
                            if (cellValue != null)
                            {
                                excelEdit.SetCellValue(currSheet, j + 3, i + 1, cellValue);//设置标题

                            }
                        }

                    }
                    excelEdit.SaveAs(selectName);
                    MessageBox.Show(inFileName+"导出成功","确定",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            catch (Exception exxx)
            {

                MessageBox.Show(inFileName + "导出失败，请重试" + exxx.StackTrace, "确定", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //throw exxx;
            }
            finally {
                excelEdit.Close();
            }

        }

        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="inFileName">导出的文件名字 </param>
        /// <param name="inGridView">导出哪儿个grid</param>
        public static void ExportToExcel(string inFileName,System.Data.DataTable inTable)
        {
            ExcelEdit excelEdit = new ExcelEdit();
            excelEdit.Create();
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "电子表格(*.xls)|*.xls";
                //saveFileDialog.AddExtension = true;
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.Title = "导出Excel文件到";
                saveFileDialog.FileName = inFileName;
                if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string selectName = saveFileDialog.FileName;
                    Worksheet currSheet = excelEdit.AddSheet(inFileName);
                    excelEdit.SetCellTatolProperty(currSheet, 1, 1, 1, 1);//设置导出的文件的名字
                    excelEdit.UniteCells(currSheet, 1, 1, 1, inTable.Columns.Count);
                    excelEdit.SetCellValueTitle(currSheet, 1, 1, inFileName);
                    //写入列标题
                    for (int i = 0; i < inTable.Columns.Count; i++)
                    {
                        excelEdit.SetCellValueTitle(currSheet, 2, i + 1, inTable.Columns[i].Caption);//设置标题

                    }
                    //写入值
                    for (int i = 0; i < inTable.Columns.Count; i++)
                    {
                        for (int j = 0; j < inTable.Rows.Count; j++)
                        {
                            var cellValue = inTable.Rows[j][i];
                            if (cellValue != null)
                            {
                                excelEdit.SetCellValueText(currSheet, j + 3, i + 1, cellValue);//设置内容

                            }
                        }

                    }
                    excelEdit.SaveAs(selectName);
                    MessageBox.Show(inFileName + "导出成功", "确定", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception exxx)
            {
                MessageBox.Show(inFileName + "导出失败，请重试", "确定", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                excelEdit.Close();
            }

        }

    }
}
